package com.example.houseman.service.impl;

import com.example.houseman.entity.TSfFinalStatementDetail;
import com.example.houseman.entity.TSfTermination;
import com.example.houseman.mapper.TSfTerminationMapper;
import com.example.houseman.service.ITSfFinalStatementDetailService;
import com.example.houseman.service.ITSfTerminationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.org.dto.SfFinalStatementDetailDto;
import com.org.dto.TSfTerminationDto;
import com.org.vo.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龚梦
 * @since 2021-04-02
 */
@Service
public class TSfTerminationServiceImpl extends ServiceImpl<TSfTerminationMapper, TSfTermination> implements ITSfTerminationService {

    @Autowired
    private TSfTerminationMapper tSfTerminationMapper;

    @Autowired
    private ITSfTerminationService sfTerminationService;

    @Autowired
    private ITSfFinalStatementDetailService sfFinalStatementDetailService;

    @Override
    public PageInfo<TSfTermination> getlist(TSfTerminationDto tSfTerminationDto) {
        PageHelper.startPage(tSfTerminationDto.getPageNum(),tSfTerminationDto.getPageSize());
        List<TSfTermination> list =  tSfTerminationMapper.getlist(tSfTerminationDto);
        return new PageInfo<>(list);
    }

    @Override
    //yingshou应收 yingtui应退
    public ResultEntity savetermination(TSfTerminationDto sfTerminationDto) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String time = sdf.format(date);
        String counta="SFJY";
        String countb="10000";
        Random random=new Random();
        int fourInt = (int)(Math.random()*(9999-1000+1))+1000;
        TSfTermination sfTermination=new TSfTermination();
        BeanUtils.copyProperties(sfTerminationDto,sfTermination);
        sfTermination.setSfTerminationNo(counta+time+countb+fourInt);
        sfTermination.setCreateTime(new Date());
        sfTermination.setUpdateTime(new Date());
        sfTermination.setInputDate(new Date());
        sfTermination.setAuditDate(new Date());
        sfTermination.setRealTerminationDate(new Date());
        sfTermination.setTerminationDate(new Date());
        sfTerminationService.save(sfTermination);
        if (sfTerminationDto.getSfFinalStatementDetailLists().size()>0){
            for (SfFinalStatementDetailDto yingshou : sfTerminationDto.getSfFinalStatementDetailLists()) {
                TSfFinalStatementDetail sfFinalStatementDetail=new TSfFinalStatementDetail();
                BeanUtils.copyProperties(yingshou,sfFinalStatementDetail);
                sfFinalStatementDetail.setSfContractNo(sfTermination.getSfContractNo());
                sfFinalStatementDetail.setCreateUserid(sfTermination.getCreateUserid());
                sfFinalStatementDetail.setSfTerminationNo(sfTermination.getSfTerminationNo());
                sfFinalStatementDetail.setTSfTerminationId(sfTermination.getId());
                sfFinalStatementDetail.setSfContractId(sfTermination.getSfContractId());
                sfFinalStatementDetail.setBusinessId(sfTermination.getBusinessId());
                sfFinalStatementDetail.setRemark(yingshou.getRemark());
                sfFinalStatementDetail.setItemType(yingshou.getItemType());
                sfFinalStatementDetail.setAmount(new BigDecimal(yingshou.getAmount().doubleValue()));
                sfFinalStatementDetail.setDealType(2);
                sfFinalStatementDetail.setCreateTime(new Date());
                sfFinalStatementDetail.setUpdateTime(new Date());
                sfFinalStatementDetailService.save(sfFinalStatementDetail);
            }
        }
        if (sfTerminationDto.getSfFinalStatementDetailList().size()>0){
            for (SfFinalStatementDetailDto yingtui : sfTerminationDto.getSfFinalStatementDetailList()) {
                TSfFinalStatementDetail sfFinalStatementDetail=new TSfFinalStatementDetail();
                BeanUtils.copyProperties(yingtui,sfFinalStatementDetail);
                sfFinalStatementDetail.setSfContractNo(sfTermination.getSfContractNo());
                sfFinalStatementDetail.setCreateUserid(sfTermination.getCreateUserid());
                sfFinalStatementDetail.setSfTerminationNo(sfTermination.getSfTerminationNo());
                sfFinalStatementDetail.setTSfTerminationId(sfTermination.getId());
                sfFinalStatementDetail.setSfContractId(sfTermination.getSfContractId());
                sfFinalStatementDetail.setBusinessId(sfTermination.getBusinessId());
                sfFinalStatementDetail.setRemark(yingtui.getRemark());
                sfFinalStatementDetail.setItemType(yingtui.getItemType());
                sfFinalStatementDetail.setAmount(new BigDecimal(yingtui.getAmount().doubleValue()));
                sfFinalStatementDetail.setDealType(1);
                sfFinalStatementDetail.setCreateTime(new Date());
                sfFinalStatementDetail.setUpdateTime(new Date());
                sfFinalStatementDetail.setCreateUserid(1L);//todo 后期修改
                sfFinalStatementDetailService.save(sfFinalStatementDetail);
            }
        }

        return ResultEntity.ok("200","解约协议录入成功");
    }

}
